'use strict';
const db = uniCloud.database()
const coll = db.collection('userinfo')
exports.main = async (event, context) => {
	if (event.sign == 1) {
		//微信登录
		//先查询openid 没有就新建用户 返回相关资料
		let res1 = await coll.where({
			name: event.name
		}).count()
		console.log(res1.total)
		if (res1.total > 0) {
			let res4 = await coll.where({
				name: event.name
			}).get()
			return res4
		} else {
			let newobj = Object.assign(event, {
				total: 0
			})
			let res2 = await coll.add(newobj)
			console.log(res2)
			let res3 = await coll.where({
				_id: res2.id
			}).get()
			return res3
		}

	} else {
		//普通登录
		console.log('普通登录')
		console.log(event)
		let res5 = await coll.where({
			name: event.name,
			password: event.password
		}).count()
		if (res5.total > 0) {
			let res6 = await coll.where({
				name: event.name,
				password: event.password
			}).get()
			return res6
		} else {
			return {
				status: 0,
				msg: '账号密码错误,请重试！'
			}
		}
	}
};
